package cn.wolfcode.rbac.web.controller;

import cn.wolfcode.rbac.domain.CourseOrder;
import cn.wolfcode.rbac.domain.Employee;
import cn.wolfcode.rbac.domain.SystemDictionaryItem;
import cn.wolfcode.rbac.query.CourseOrderQuery;
import cn.wolfcode.rbac.service.ICourseOrderService;
import cn.wolfcode.rbac.service.IEmployeeService;
import cn.wolfcode.rbac.service.ISystemDictionaryItemService;
import cn.wolfcode.rbac.util.JsonResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/courseOrder")
public class CourseOrderController {
    
    @Autowired
    private ICourseOrderService courseOrderService;
    
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private IEmployeeService employeeService;

    // 提供一个方法处理查询所有部门数据请求，响应 HTML
    @RequestMapping("/list")
    // qo 要贴注解，@ModelAttribute 将数据添加到模型对象中，等价于 model.addAttribute("attributeName", qo);
    public String list(Model model,@ModelAttribute("qo") CourseOrderQuery qo){
        List<SystemDictionaryItem> subjects = systemDictionaryItemService.selecteByDiSn("subject");
        model.addAttribute("subjects",subjects);

        List<Employee> employees = employeeService.listAll();
        model.addAttribute("employees",employees);

        PageInfo result = courseOrderService.query(qo);
        model.addAttribute("result", result);
        return "courseOrder/list"; // /WEB-INF/views/courseOrder/list3.ftl
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody   // 告诉 Spring MVC，我们需要转为 json格式返回给前端
    public JsonResult saveOrUpdate(CourseOrder courseOrder){

            if(courseOrder.getId() == null){ // 代表新增
                courseOrderService.save(courseOrder);
            }else { // 代表修改
                courseOrderService.update(courseOrder);
            }
            return new JsonResult();
        //return "redirect:/courseOrder/list.do";  // http://localhost/courseOrder/list.do
    }


    @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id){

            if (id != null) {
                courseOrderService.delete(id);
            }
            return new JsonResult();
        // return "redirect:/courseOrder/list.do";  // http://localhost/courseOrder/list.do
    }
}
